package homework0713;

import homework07011_2.DouDiZhuMain;
import util.Util;

import javax.swing.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class DouDiZhuGUIMain extends JFrame {

    private List<homework0713.Card> poker = new ArrayList<>();

    public DouDiZhuGUIMain() {
        //设置窗口大小
        this.setSize(1100, 800);

        //设置title
        this.setTitle("斗地主");

        //设置窗体居中，需要写在窗口大小设置后
        //this.setLocationRelativeTo(null);

        //设置窗口点击关闭程序结束
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        //设置窗口可见
        this.setVisible(true);
    }

    public static void main(String[] args) {
        DouDiZhuGUIMain douDiZhuGUIMain = new DouDiZhuGUIMain();
        douDiZhuGUIMain.createTheCard();
        douDiZhuGUIMain.shufflePoker();
        douDiZhuGUIMain.Dea();
    }

    //生成牌堆
    public void createTheCard() {
        String point[] = "3,4,5,6,7,8,9,10,11,12,13,14,15".split(",");
        String type[] = "1,2,3,4".split(",");
        for (int i = 0; i < point.length; i++) {
            for (int j = 0; j < type.length; j++) {
                poker.add(new homework0713.Card(type[j], point[i], i, j, "/image/" + (i + 3) + "_" + (j + 1) + ".jpg"));
            }
        }
        poker.add(new homework0713.Card("16", "1", 16, 0, "/image/" + 16 + "_" + 1 + ".jpg"));
        poker.add(new homework0713.Card("17", "1", 17, 0, "/image/" + 17 + "_" + 1 + ".jpg"));
       // System.out.println(poker);
    }

    //洗牌
    public void shufflePoker() {
        Collections.shuffle(poker);
       // System.out.println(poker);
    }

    //发牌
    public void Dea() {
        List<homework0713.Card> player1 = poker.subList(0, 17);//得到玩家一的牌集合
        List<homework0713.Card> player2 = poker.subList(17, 34);//得到玩家二的牌集合
        List<homework0713.Card> player3 = poker.subList(34, 51);//得到玩家三的牌集合
        List<homework0713.Card> cardPoker = poker.subList(51, 54);//得到底牌
        sort(player1);
        sort(player2);
        sort(player3);
        sort(cardPoker);
       // System.out.println("玩家一:" + player1 + "\n玩家二:" + player2 + "\n玩家三:" + player3 + "\n底牌:" + cardPoker);
        int height1 = 580;
        for (int i = 0; i < player1.size(); i++) {
            Icon pokerImage = Util.getIcon(player1.get(i).getImageLocation());
            JLabel imageJLabel = new JLabel(pokerImage);
            imageJLabel.setBounds(0, height1, 105, 150);
            this.add(imageJLabel);
            height1 -= 35;
        }
        int height2 = 730;
        for (int i = 0; i < player2.size(); i++) {
            Icon pokerImage = Util.getIcon(player2.get(i).getImageLocation());
            JLabel imageJLabel = new JLabel(pokerImage);
            imageJLabel.setBounds(height2, 575, 105, 150);
            this.add(imageJLabel);
            height2 -= 35;
        }
        int height3 = 580;
        for (int i = 0; i < player3.size(); i++) {
            Icon pokerImage = Util.getIcon(player3.get(i).getImageLocation());
            JLabel imageJLabel = new JLabel(pokerImage);
            imageJLabel.setBounds(900, height3, 105, 150);
            this.add(imageJLabel);
            height3 -= 35;
        }
        int height4 = 550;
        for (int i = 0; i < cardPoker.size(); i++) {
            Icon pokerImage = Util.getIcon(cardPoker.get(i).getImageLocation());
            JLabel imageJLabel = new JLabel(pokerImage);
            imageJLabel.setBounds(height4, 0, 105, 150);
            this.add(imageJLabel);
            height4 -= 35;
        }
    }

    //排序
    public void sort(List list) {
        list.sort(new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                if (o1.getPointIndex() == o2.getPointIndex()) {
                    return o1.getTypeIndex() - o2.getTypeIndex();
                } else {
                    return o1.getPointIndex() - o2.getPointIndex();
                }
            }
        });
    }
}
